package com.dailywork.basepro.math;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 相对排序
 *
 * @author think
 * @version 1.0.0
 * @since 2019/12/22
 */
public class RelativeSort {
    public static void main(String[] args) {
        int[][] array = {{8, 0}, {5, 2}, {6, 1}, {7, 2}, {4, 4}, {8, 1}};
        int[][] result = relativeSort(array);
        for (int[] ary : result) {
            System.out.println(Arrays.toString(ary));
        }
    }

    private static int[][] relativeSort(int[][] array) {
        Arrays.sort(
                array,
                (o1, o2) -> {
                    if (o1[0] != o2[0]) {
                        return o2[0] - o1[0];
                    } else {
                        return o1[1] - o2[1];
                    }
                });
        List<int[]> list = new ArrayList<>();
        for (int[] ary : array) {
            list.add(ary[1], ary);
        }
        return list.toArray(new int[][]{});
    }
}
